package com.example.sxproject.controller;
import com.example.sxproject.pojo.*;
import com.example.sxproject.service.*;
import com.google.gson.Gson; //JSON转String的依赖
import com.google.gson.Gson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/mananger")
public class ManangerController {


    @Autowired
    private ManangerService manangerService;

    @Autowired
    private UserService userService;

    @Autowired
    private LoanService loanService;

    @Autowired
    private CompanyService companyService;


    @Autowired
    private FinancialService financialService;


    @GetMapping("/m_login")
    public String login(String phone, String password, RedirectAttributes redirectAttributes) {
        // 查询用户
        Mananger m = manangerService.findByManangerP_P(phone, password);
        System.out.println("输出：" + m);

        if (m != null) {
            // 存在符合该账户密码的账号
            // 可以将用户信息放入 session 或者其他范围中
            // redirectAttributes.addFlashAttribute("message", "登录成功");
            redirectAttributes.addFlashAttribute("message", "登录成功");
            return "index2";
        } else {
            // 不存在该账号和密码
            // 可以将错误信息放入模型或重定向属性中
            redirectAttributes.addFlashAttribute("message", "登录失败");
            return "mlogin";
        }
    }

    //增加贷款产品
    @PostMapping("loanProductMessageAdd")
    public String loanProductMessageAdd(LoanProduct loanProduct) {
        loanService.addLoanProduct(loanProduct);
        return "loanMessage";
    }

    //添加公司
    @PostMapping("/companyMessageAdd")
    public String companyMessageAdd(Company company) {
        companyService.addCompany(company);
        return "companyMessage";
    }

    @PostMapping("/addFinanceMessage")
    public String addFinanceMessage(FinancialProduct financialProduct) {
        financialService.addFinancialProduct(financialProduct);
        return "financeMessage";

    }


    //添加用户信息
    @PostMapping("/addUserMessage")
    public String addUserMessage(User user) {
        String phone = user.getPhone();
        User u = userService.findByPhone(phone);
        if (u == null) {
            userService.addUserMessage(user);
            return "userMessage";
        } else {
            return "addUserMessage";
        }

    }

}
